﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web;
using Super.DomainService;
using Super.Entities;
using Super.Common;
using Super.Common.Security;
using System.ComponentModel;
using System.Linq;
using Super.Infrastructure;

namespace Super.Filter
{

    /// <summary>
    /// 前台用户登录验证过滤器
    /// </summary>
    public class UserLoginAuthorize : AuthorizeAttribute
    {
        private bool _check = true;

        private bool _isAjax = false;

        /// <summary>
        /// 默认检查所有
        /// </summary>
        public bool Check { get { return _check; } set { this._check = value; } }

        public bool Ajax { get { return _isAjax; } set { this._isAjax = value; } }


        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            var currentActionAttribute = filterContext.ActionDescriptor.GetCustomAttributes(typeof(UserLoginAuthorize), false); //获取当前Action的重叠登录过滤器

            if (currentActionAttribute.Length > 0)
            {
                var userLoginAuthorize = currentActionAttribute[0] as UserLoginAuthorize;
                if (userLoginAuthorize != null)
                    Check = userLoginAuthorize.Check;  //读取设置重叠过滤器的Check属性
            }

            //Check属性设置为false时，不进行检查.
            if (Check)
            {
                var urlDomain = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var enCodeUrl = Hex.Encrypt(urlDomain);

                var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
                var action = filterContext.RouteData.Values["action"].ToString().ToLower();
                if (CookieService.GetUserID() <= 0)
                {
                    if (Ajax)
                    {
                        filterContext.RequestContext.HttpContext.Response.Write("nologin");
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect(SuperService.WebUrl + "/login?r=" + enCodeUrl);
                    }

                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                }
            }
        }
      
    }

    /// <summary>
    /// 论坛管理员权限
    /// </summary>
    public class ForumManageAuthorize : AuthorizeAttribute
    {
        private bool _check = true;
        private bool _isAjax = false;
        /// <summary>
        /// 默认检查所有
        /// </summary>
        public bool Check { get { return _check; } set { this._check = value; } }

        public bool Ajax { get { return _isAjax; } set { this._isAjax = value; } }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            var currentActionAttribute = filterContext.ActionDescriptor.GetCustomAttributes(typeof(UserLoginAuthorize), false); //获取当前Action的重叠登录过滤器

            if (currentActionAttribute.Length > 0)
            {
                var userLoginAuthorize = currentActionAttribute[0] as UserLoginAuthorize;
                if (userLoginAuthorize != null)
                    Check = userLoginAuthorize.Check;  //读取设置重叠过滤器的Check属性
            }

            //Check属性设置为false时，不进行检查.
            if (Check)
            {
                var urlDomain = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var enCodeUrl = Hex.Encrypt(urlDomain);

                var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
                var action = filterContext.RouteData.Values["action"].ToString().ToLower();


                if (CookieService.GetUserID() <= 0)
                {
                    if (Ajax)
                    {
                        filterContext.RequestContext.HttpContext.Response.Write("nologin");
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect(SuperService.WebUrl + "/login?r=" + enCodeUrl);
                    }
                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                    return;
                }

                var loginUser = SuperService.LoginUser.User;

                if (loginUser == null)
                {
                    if (Ajax)
                    {
                        filterContext.RequestContext.HttpContext.Response.Write("nologin");
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect(SuperService.WebUrl + "/login?r=" + enCodeUrl);
                    }

                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                    return;
                }

              

            }
        }
    }


    /// <summary>
    /// 站点管理员权限
    /// </summary>
    public class SiteManageAuthorize : AuthorizeAttribute
    {
        private bool _check = true;
        private bool _isAjax = false;
        /// <summary>
        /// 默认检查所有
        /// </summary>
        public bool Check { get { return _check; } set { this._check = value; } }

        public bool Ajax { get { return _isAjax; } set { this._isAjax = value; } }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            var currentActionAttribute = filterContext.ActionDescriptor.GetCustomAttributes(typeof(UserLoginAuthorize), false); //获取当前Action的重叠登录过滤器

            if (currentActionAttribute.Length > 0)
            {
                var userLoginAuthorize = currentActionAttribute[0] as UserLoginAuthorize;
                if (userLoginAuthorize != null)
                    Check = userLoginAuthorize.Check;  //读取设置重叠过滤器的Check属性
            }

            //Check属性设置为false时，不进行检查.
            if (Check)
            {
                var urlDomain = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var enCodeUrl = Hex.Encrypt(urlDomain);

                var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
                var action = filterContext.RouteData.Values["action"].ToString().ToLower();


                if (CookieService.GetUserID() <= 0)
                {
                    if (Ajax)
                    {
                        filterContext.RequestContext.HttpContext.Response.Write("nologin");
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect(SuperService.WebUrl + "/admin/login?r=" + enCodeUrl);
                    }
                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                    return;
                }

                var loginUser = SuperService.LoginUser.User;

                if (loginUser == null)
                {
                    if (Ajax)
                    {
                        filterContext.RequestContext.HttpContext.Response.Write("nologin");
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect(SuperService.WebUrl + "/admin/login?r=" + enCodeUrl);
                    }
                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                    return;
                }

               

            }
        }
    }

   

}
